﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.events.ListEvent;
	import flash.text.TextFormat;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolMail extends MovieClip
	{
		public static const NAME:String = "Tool-Mail";

		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var mailList:DataGrid;
		
		public function ToolMail()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			mailList = new DataGrid();
			mailList.x = 5;
			mailList.y = 70;
			mailList.width = 590;
			mailList.height = 310;
			this.addChild(mailList);
			
			mailList.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onInfo);
			
			btnDelete.visible = false;
			btnSelectOther.visible = false;
			btnSelectAll.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnDelete.addEventListener(MouseEvent.CLICK, onClick);
			btnSelectOther.addEventListener(MouseEvent.CLICK, onClick);
			btnSelectAll.addEventListener(MouseEvent.CLICK, onClick);
			btnWrite.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			if (params.Name == "GetRoleMail")
			{
				var style:TextFormat = new TextFormat();
				style.color = 0xff6600;
				style.size = 16;
				style.font = "隶书";
			
				mailList.setRendererStyle("textFormat", style);
			
				var c0:DataGridColumn = new DataGridColumn("ID");
				var c1:DataGridColumn = new DataGridColumn("Role");
				var c2:DataGridColumn = new DataGridColumn("寄件人");
				var c3:DataGridColumn = new DataGridColumn("标题");
				var c4:DataGridColumn = new DataGridColumn("时间");
				var c5:DataGridColumn = new DataGridColumn("备注");
			
				c0.visible = false;
				c1.visible = false;
				c2.width = 90;
				c3.width = 300;
				c4.width = 100;
				c5.width = 100;
				
				mailList.addColumn(c0);
				mailList.addColumn(c1);
				mailList.addColumn(c2)
				mailList.addColumn(c3);
				mailList.addColumn(c4);
				mailList.addColumn(c5);
				
				var currDate:Date;
				var D:int, M:int, Y:int;
				var theDate:String;

				for (var i:int = 0; i < params.Process.length; i++)
				{
					currDate = new Date(params.Process[i].CreateTime);
					
					Y = currDate.getFullYear();
					M = currDate.getMonth() + 1;
					D = currDate.getDate();

					theDate = (Y + "-" + M + "-" + D);
					
					mailList.addItem({"ID":params.Process[i].SID_Mail, "Role":params.Process[i].Role_ID, "寄件人":params.Process[i].RoleName, "标题":params.Process[i].Title, "时间":theDate, "备注":"双击查看"});
				}
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnDelete.removeEventListener(MouseEvent.CLICK, onClick);
			btnSelectOther.removeEventListener(MouseEvent.CLICK, onClick);
			btnSelectAll.removeEventListener(MouseEvent.CLICK, onClick);
			btnWrite.removeEventListener(MouseEvent.CLICK, onClick);
			
			mailList.removeEventListener(ListEvent.ITEM_DOUBLE_CLICK, onInfo);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onInfo(evt:ListEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);

			dispatchEvent(new SwcEvent(NAME, ["-showBox", ["mailInfo", evt.item["ID"]]]));
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolMail"]));
					break;
				case "btnDelete":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolMail"]));
					break;
				case "btnSelectOther":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolMail"]));
					break;
				case "btnSelectAll":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolMail"]));
					break;
				case "btnWrite":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["mailNew"]]));
					break;
			}
		}
	}
}